
--This function name and notes cannot be modified
--@@funCfg: <joinUs, noexist>
function joinUs()
------------USER CODE DATA--------------
lvgl.style_set_bg_color(Style_LvglButton2_1, lvgl.STATE_DEFAULT, lvgl.color_hex(0xFF0000))
lvgl.style_set_value_str(Style_LvglButton2_1, lvgl.STATE_DEFAULT, "逗你的，还是手机扫码加群一起玩耍吧")
lvgl.style_set_value_color(Style_LvglButton2_1, lvgl.STATE_DEFAULT, lvgl.color_hex(0x1A1A1A))
lvgl.obj_add_style(ScreenA.LvglButton2.self, lvgl.BTN_PART_MAIN, Style_LvglButton2_1)
------------USER CODE DATA--------------
end


--This function name and notes cannot be modified
--@@funCfg: <reset, noexist>
function reset()
------------USER CODE DATA--------------
lvgl.style_set_bg_color(Style_LvglButton2_1, lvgl.STATE_DEFAULT, lvgl.color_hex(0x0088FF))
lvgl.style_set_value_str(Style_LvglButton2_1, lvgl.STATE_DEFAULT, "Hello LuatOS UI")
lvgl.obj_add_style(ScreenA.LvglButton2.self, lvgl.BTN_PART_MAIN, Style_LvglButton2_1)
lvgl.style_set_value_color(Style_LvglButton2_1, lvgl.STATE_DEFAULT, lvgl.color_hex(0xFFFFFF))
------------USER CODE DATA--------------
end--This function name and notes cannot be modified
--@@funCfg: <dir_up, exist>
local curTws = "up"
local snakeTmId = nil
local staV = false
local disCont, snakeHead
local bodyBeEat = false
--吃豆人初始位置
local snakePos_x, snakePos_y = 240, 560
local beanSpeed = 200
local dif_x, dif_y = 100, 100
local score = "0"


function dir_up()
------------USER CODE DATA--------------
curTws = "up"
------------USER CODE DATA--------------
end


--This function name and notes cannot be modified
--@@funCfg: <dir_left, exist>
function dir_left()
------------USER CODE DATA--------------
curTws = "left"
------------USER CODE DATA--------------
end


--This function name and notes cannot be modified
--@@funCfg: <dir_right, exist>
function dir_right()
------------USER CODE DATA--------------
curTws = "right"
------------USER CODE DATA--------------
end


--This function name and notes cannot be modified
--@@funCfg: <dir_down, exist>
function dir_down()
------------USER CODE DATA--------------
curTws = "down"
------------USER CODE DATA--------------
end


--吃豆人的移动逻辑
local function snakeMovLgc()
    if curTws == "up" then
        snakePos_y = snakePos_y - 5
    elseif curTws == "left" then
        snakePos_x = snakePos_x - 5
    elseif curTws == "down" then
        snakePos_y = snakePos_y + 5
    elseif curTws == "right" then
        snakePos_x = snakePos_x + 5
    end
    if (snakePos_x < 0 or snakePos_x > 480 or snakePos_y < 0 or snakePos_y > 570)then
        gameover()
    end
end


--随机豆子生成
local rand_x = {60, 40, 240, 340, 390, 440}
local rand_y = {20, 80, 150, 220, 300, 356, 460, 500, 550}
local rand_xx = 1
local rand_yy = 1

local function beanmove()
    bodyBeEat = true
	if (rand_xx > 6) then
		rand_xx = rand_xx - 6
		beanPos_x = rand_x[rand_xx]
	else
		beanPos_x = rand_x[rand_xx]
	end
	if (rand_yy > 9)then 
		rand_yy = rand_yy - 9
		beanPos_y = rand_y[rand_yy]
	else
		beanPos_y = rand_y[rand_yy]
	end
	rand_xx = rand_xx + 1
	rand_yy = rand_yy + 1

    log.info("beanmove  x,y",beanPos_x, beanPos_y)
    lvgl.obj_set_pos(game.LvglImgmnck.self, beanPos_x, beanPos_y)
end


--吃豆人的积分计算
local function getBean()
    dif_x = snakePos_x - beanPos_x
    dif_y = snakePos_y - beanPos_y
    if ((dif_x * dif_x) + (dif_y * dif_y) < 900)then

        bodyBeEat = false
        local tem = tonumber(score)
        tem = tem + 10
        score = tostring(tem)
        lvgl.label_set_text(game.LvglLabelscore.self, score)

        if (tem%30 == 0)then
            sys.timerStop(snakeTmId)
            snakeTmId = nil
            if beanSpeed >100 then beanSpeed = beanSpeed - 10 end          
            snakeTmId = sys.timerLoopStart(sankeMoving, beanSpeed)
        end
    end
end

--吃豆人移动
function sankeMoving()
    log.info("ljd sankeMoving enter")
    if not game.contFather_game  then return end
    if (not bodyBeEat)then
        beanmove()
    end
    snakeMovLgc()
    log.info("ljd sankeMoving x,y",snakePos_x,snakePos_y,game.LvglImghead.self)
    
    lvgl.obj_set_pos(game.LvglImghead.self, snakePos_x,snakePos_y)
    if ((curTws == "up"))then
        if (not staV)then
            lvgl.img_set_src(game.LvglImghead.self, "/lua/beanU2.jpg")
            lvgl.obj_set_pos(game.LvglImghead.self,snakePos_x,snakePos_y)
            staV = true
        else
            lvgl.img_set_src(game.LvglImghead.self, "/lua/beanU.jpg")
            lvgl.obj_set_pos(game.LvglImghead.self, snakePos_x,snakePos_y)
            staV = false
        end
    elseif (curTws == "left") then
        if (not staV)then
            lvgl.img_set_src(game.LvglImghead.self, "/lua/beanL2.jpg")
            lvgl.obj_set_pos(game.LvglImghead.self, snakePos_x,snakePos_y)
            staV = true
        else
            lvgl.img_set_src(game.LvglImghead.self, "/lua/beanL.jpg")
            lvgl.obj_set_pos(game.LvglImghead.self, snakePos_x,snakePos_y)
            staV = false
        end
    elseif (curTws == "down") then
        if (not staV)then
            lvgl.img_set_src(game.LvglImghead.self, "/lua/beanD2.jpg")
            lvgl.obj_set_pos(game.LvglImghead.self, snakePos_x,snakePos_y)
            staV = true
        else
            lvgl.img_set_src(game.LvglImghead.self, "/lua/beanD.jpg")
            lvgl.obj_set_pos(game.LvglImghead.self, snakePos_x,snakePos_y)
            staV = false
        end
    elseif (curTws == "right") then
        if (not staV)then
            lvgl.img_set_src(game.LvglImghead.self, "/lua/beanR2.jpg")
            lvgl.obj_set_pos(game.LvglImghead.self, snakePos_x,snakePos_y)
            staV = true
        else
            lvgl.img_set_src(game.LvglImghead.self, "/lua/beanR.jpg")
            lvgl.obj_set_pos(game.LvglImghead.self, snakePos_x,snakePos_y)
            staV = false
        end
    end
    getBean()
end


function gameover()
    print("gameover", snakeTmId)
    sys.timerStop(snakeTmId)
    snakeTmId = nil
    -- game.free()
    curTws = "up"
    snakePos_x = 240
    snakePos_y = 560
    beanSpeed = 200

    bodyBeEat = false
    score = "0"
    print("snakePos_x, snakePos_y", snakePos_x, snakePos_y)
    gmover.create()
end

--This function name and notes cannot be modified
--@@funCfg: <startgame, exist>
function startgame()
------------USER CODE DATA--------------
    P1.free()

    game.create()
    log.info("startgame",game.contFather_game,snakeTmId)
    if not game.contFather_game or snakeTmId then 
        log.info("startgame return")
        return
    end

    snakeHead = game.LvglImghead.self
    disCont = game.contFather_game
    snakeTmId = sys.timerLoopStart(sankeMoving, beanSpeed)
------------USER CODE DATA--------------
end



--This function name and notes cannot be modified
--@@funCfg: <gameagain, exist>
function gameagain()
------------USER CODE DATA--------------
    gmover.free()

    game.create()
    log.info("gameagain",game.contFather_game,snakeTmId)
    if not game.contFather_game or snakeTmId then 
        log.info("gameagain return")
        return
    end

    snakeHead = game.LvglImghead.self
    disCont = game.contFather_game
    snakeTmId = sys.timerLoopStart(sankeMoving, beanSpeed)
------------USER CODE DATA--------------
end




--This function name and notes cannot be modified
--@@funCfg: <game, noexist>
function game()
------------USER CODE DATA--------------

------------USER CODE DATA--------------
end


